#ifdef COMPILE_OPENGL

#include "COGL_Extensions.h"
#include "CBL_CRC32.h"
#include <OpenGL/OpenGL.h>
#include <OpenGL/glext.h>

CB_SUBNAMESPACE_START(OpenGL)

CExtensions::CExtensions()
{
	// "explode" the extension string so that each item in the vector
	// starts with a new extension.
	
	const char* pExtensionString = (const char*)glGetString(GL_EXTENSIONS);
	m_ExtensionStrings.reserve(100);
	
	const char* CharIter = pExtensionString;
	
	while (*CharIter != 0)
	{
		++CharIter;
		uint32 NextExtension = strcspn(CharIter, " ");
		
		SStringChunk Chunk;			
		Chunk.m_pChunk = CharIter;
		Chunk.m_Length = NextExtension;
		
		m_ExtensionStrings.push_back(Chunk);
		
		CharIter += NextExtension;
	}	
}

// ---------------------------------------------------------------------------------------------------------------------

CExtensions::~CExtensions()
{
}

// ---------------------------------------------------------------------------------------------------------------------

bool CExtensions::IsSupported(const char* _pExtensionName)
{
	uint32 ExtensionId = Math::CRC32(_pExtensionName);
	
	CRegistryMap::const_iterator ElementIter = m_Registry.find(ExtensionId);
	
	if (ElementIter != m_Registry.end())
		return ElementIter->second; // ### return ###
	
	CStringVector::iterator ExtStrIter = m_ExtensionStrings.begin();
	const CStringVector::iterator ExtStrEnd = m_ExtensionStrings.end();
	
	while (ExtStrIter != ExtStrEnd)
	{
		if (strncmp(ExtStrIter->m_pChunk, _pExtensionName, ExtStrIter->m_Length) == 0)
		{
			m_Registry[ExtensionId] = true;
			return true; // ### return ###
		}
	}
	
	m_Registry[ExtensionId] = false;
	return false;
}

CB_SUBNAMESPACE_END

#endif